<?php

class Default_AuthController extends Zend_Controller_Action {

    public function indexAction() {                
        $form = new Benservi_Forms_Auth();        
        $this->_helper->layout->setLayout("layoutlogin");

        if ($this->_request->isPost()) {
            $data = $this->_request->getPost();
               if ($form->isValid($data)) {
            
                $authAdapter = $this->getAuthAdapter();
                $authAdapter->setIdentity($data ['email'])
                        ->setCredential($data ['password']);

                $result = $authAdapter->authenticate();

                if ($result->isValid()) {
                    $auth = Zend_Auth::getInstance ();
                    $auth->setStorage(new Zend_Auth_Storage_Session('default'));
                    $dataAuth = $authAdapter->getResultRowObject(null, 'senha');
                    $auth->getStorage()->write($dataAuth);


                    $usuario = Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_Session("default"))->getStorage()->read();
                    $usuario = $usuario->nome;
                    $mail = new Zend_Mail();

                    $mail->setBodyText('This is the text of the mail.')
                          ->setFrom('gustavo@meredyk.com.br', 'Benservi')
                          ->addTo('gustavo@meredyk.com.br', 'Login efetuado')
                          ->setSubject('Acesso ao sistema')
                          ->setBodyHtml('<font face="arial">O usuario <b>'.$usuario.'</b> logou no sistema as <b>'.date('H:m:s').'</b> do dia <b>'.date('d/m/Y').'</b></font>')
                          ->send();

                    $this->_redirect("/");
                } else {
                    echo '<div class=error>';
                    echo "Usuário ou senha inválidos";
                    echo '</div>';
                }
                } else {            
                    $elementMessages = $form->getMessages();                   
                    if (is_array($elementMessages))
                    {
                        echo '<div class=error>';
                        foreach ($elementMessages as $element)
                        {
                            foreach ($element as $message)
                            {

                                echo $message.'<br >';
                            }
                        }
                        echo '</div>';
                    }
            }
            
        }
        $this->view->form = $form;
    }

    private function getAuthAdapter() {
        $bootstrap = $this->getInvokeArg('bootstrap');
        $resource = $bootstrap->getPluginResource('db');
        $db = $resource->getDbAdapter();
        $authAdapter = new Zend_Auth_Adapter_DbTable($db);
        $authAdapter->setTableName('acesso')->setIdentityColumn('email')
                ->setCredentialColumn('passwords')
                ->setCredentialTreatment('SHA1(?)');
        return $authAdapter;
    }

    public function logoutAction() {
        $auth = Zend_Auth::getInstance ();
        $auth->setStorage(new Zend_Auth_Storage_Session('default'));
        $auth->clearIdentity();
        $this->_redirect("/");
    }

}

